package com.music.mapper.music;

import com.music.dto.music.PlaylistDTO;
import com.music.dto.tag.TagDTO;
import com.music.entity.collection.CollectionPlaylistEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CollectionPlaylistMapper {


    /*
     * @Description 根据用户Id列出我的收藏歌单
     * @Author hbh
     * @Date 2021/12/5
     **/
    @Select("<script>select a.playlist_id, playlist_name, tag_id, playlist_info, a.image_url, " +
            "a.image_name, is_public, a.gmt_create, a.user_id, b.image_url avatar, b.user_name, " +
            "(select count(*) FROM collection_playlist WHERE playlist_id = a.playlist_id) collectors " +
            "FROM music_playlist a left join basic_user b on a.user_id = b.user_id " +
            "WHERE playlist_id IN (SELECT playlist_id FROM collection_playlist WHERE user_id = #{userId}) " +
            "AND is_public = 1 " +
            "<if test='playlistName != null'> AND playlist_name LIKE CONCAT('%', #{playlistName}, '%') </if>" +
            "LIMIT #{start}, #{length} " +
            "</script>")
    List<PlaylistDTO> list4Table(
            @Param("playlistName") String playlistName,
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("userId") Long userId) throws Exception;


    /*
     * @Description 根据用户Id查询音乐广场歌单总数
     * @Author hbh
     * @Date 2021/11/26
     **/
    @Select("<script>select count(*) " +
            "FROM music_playlist " +
            "WHERE playlist_id IN (SELECT playlist_id FROM collection_playlist WHERE user_id = #{userId}) " +
            "AND is_public = 1 " +
            "<if test='playlistName != null'> AND playlist_name LIKE CONCAT('%', #{playlistName}, '%') </if>" +
            "</script>")
    Long count4Table(@Param("userId") Long userId,
                     @Param("playlistName") String playlistName) throws Exception;

    /*
     * @Description 新增收藏歌单
     * @Author hbh
     * @Date 2021/11/26
     **/
    @Insert("Insert into collection_playlist ( " +
            "user_id, playlist_id, gmt_create) " +
            "VALUES (#{entity.userId}, #{entity.playlistId}, #{entity.gmtCreate})")
    Integer addCollectionPlaylist(@Param("entity") CollectionPlaylistEntity entity) throws Exception;


    /*
     * @Description 取消收藏歌单
     * @Author hbh
     * @Date 2021/11/26
     **/
    @Delete("Delete from collection_playlist where user_id = #{userId} and playlist_id = #{playlistId}")
    Integer removeByUserId(@Param("userId") Long userId,
                                     @Param("playlistId") Long playlistId) throws Exception;

    /*
     * @Description 查询所有子标签
     * @Author DY
     * @Date 2021/11/23
     **/
    @Select("SELECT tag_id, tag_name from basic_tag WHERE tag_type = 2 ")
    List<TagDTO> listAllTag() throws Exception;
}
